package com.nefits.ws;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.nefits.bean.SCBean;
import com.nefits.utils.XMLUtil;

public class SCEnminutDataServiceImpl implements SCEnminutDataService {
	private Date bTime;
	private Date eTime;
	private DateFormat parseFileDate = new SimpleDateFormat("yyyyMMdd");
	private DateFormat parseDataTime = new SimpleDateFormat("yyyyMMddHHmm");
	@Override
	public String getEnminutData(String beginTime, String endTime) {
		try {
			DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm");
			bTime =  df.parse(beginTime);
			eTime =  df.parse(endTime);
		} catch (ParseException e1) {
			e1.printStackTrace();
			return "error:\u65f6\u95f4\u53c2\u6570\u683c\u5f0f\u4e0d\u6b63\u786e(yyyyMMdd HH:mm)";
		}
		StringBuffer xml = new StringBuffer();
		xml.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
		xml.append("<rows>\n");
		
		if(!bTime.after(eTime)){
			String configPath = getClass().getClassLoader().getResource("/").getPath()+"config.xml";
			String lp_manager_path = XMLUtil.getValue("lp_manager_path", configPath);
			String prosData_path = lp_manager_path + File.separator+"ProsData";
			try {
				String rows = getFiles(new File(prosData_path));
				//String rows = getFiles(new File("E:\\LP-MANAGER\\ProsData\\"));
				
				xml.append(rows);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		xml.append("</rows>\n");
		return xml.toString();
	}
	
	
	
	private String getFiles(File file) throws IOException, ParseException{
		StringBuffer allScDatas = new StringBuffer();
		if(!file.isDirectory()){
			String fileName = file.getName();
			System.out.println("读到文件 :"+file.getParent()+"\\"+fileName);

			//C40和C11设备的日志文件名的格式不相同，要分别对待
			Date fileDate = parseFileDate.parse(fileName.substring(9,17));//得到文件的日期
			if(fileName.startsWith("C") && fileName.endsWith(".txt") && fileName.length()==21 && !beforeIgoreTime(fileDate,bTime) && !beforeIgoreTime(eTime,fileDate) ){
				String scDatas = readFile(file);
				allScDatas.append(scDatas);
			}
		}
		else{
			System.out.println("扫描目录："+file.getPath());
			File[] filelist = file.listFiles();
			for(File subfile:filelist){
				allScDatas.append(getFiles(subfile));
			}
		}
		return allScDatas.toString();
	}
	
	
	private String readFile(File file) throws IOException, ParseException{
		StringBuffer scbeans = new StringBuffer();
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String readoneline;
		while((readoneline = br.readLine()) != null){
			Date dataTime = parseDataTime.parse(readoneline.substring(8,20));
			if(!bTime.after(dataTime) && !eTime.before(dataTime)){
				SCBean scbean = new SCBean(readoneline);
				scbeans.append(scbean.toXmlString());
			}
			//System.out.println(readoneline);
		}
		br.close();
		fr.close();
		return scbeans.toString();
	}
	
	
	private boolean beforeIgoreTime(Date d1,Date d2) throws ParseException{
		Date tmp_d1 = parseFileDate.parse(parseFileDate.format(d1));
		Date tmp_d2 = parseFileDate.parse(parseFileDate.format(d2));
		if(tmp_d1.before(tmp_d2)){
			return true;
		}
		return false;
	}
	
	public static void main(String[] str)throws Exception{
		SCEnminutDataServiceImpl sc = new SCEnminutDataServiceImpl();
		System.out.println(sc.getEnminutData("20120810 02:50", "20120810 02:57"));
	}
}
